/*
Written by Best Flash Solutions, Inc  
See:
	http://www.bestflashsolutions.com
Contact me: contact@bestflashsolutions.com
*/

/**
 * Play list of video
 */
package com.bestflashsolutions.red5Streaming
{
	import com.bestflashsolutions.CommonUtils;
	import com.bestflashsolutions.app.AccessPermission;
	import com.bestflashsolutions.app.AppUserDataInfo;
	import com.bestflashsolutions.view.DJSingleChat;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.core.Application;
	
	public class PlayLiveList 
	{
		//Information about app
		public var objInformation: MyObject;
		public var crrPos: int;
		//Callback function when all videos have beenplaying
		public var onComplete: Function;
		public var objContainer: Canvas;
		
		private var nc: NetConnection;
		
		public var lstLiveStream: ArrayCollection = new ArrayCollection();
		
		public function PlayLiveList(nc: NetConnection, objInfo: MyObject)
		{
			objContainer = Application.application.cvsRoom;
			var i: int;
			objInformation = objInfo;
			objContainer.addChild(Application.application.objRubberBand);
			//With each joiner --> create video window
			for (i=0; i<objInformation.maxJoiner; i++) {
				//Create video window
				var objLiveStream: PlayLive = new PlayLive(nc);
				objInformation.lstVideoDisplay.addItemAt(objLiveStream.objVideoDsp, i);
				objLiveStream.netStatus = netStatus;
				//stream name - username
				objLiveStream.streamName = objInformation.lstJoiner[i];
				//Check if avaiable to receive streaming
				if (objLiveStream.streamName!="" && objLiveStream.streamName!=objInformation.username) {
					objLiveStream.available = true; 
					objLiveStream.joinDate = new Date().getTime();
					var objJoiner = objInformation.findRoomJoiner(objInformation.lstJoiner[i]);
					if (objJoiner) {
						//Save size of video
						//objLiveStream.cam_height = objJoiner.@cam_height;
						//objLiveStream.cam_width = objJoiner.@cam_width;
						objLiveStream.permission = objJoiner.@permission;
						objLiveStream.streamGuid = objJoiner.@vidguid;
						
						if(!objLiveStream.getChildByName("singleChat")){
							var showRoom:Boolean = true;
							var roomName:String = "";
							if((objInformation.permission == AccessPermission.INSTRUCTOR && objJoiner.@permission == AccessPermission.INSTRUCTOR) ||
								(objJoiner.@permission == AccessPermission.ADMINISTRATOR && objInformation.permission == AccessPermission.ADMINISTRATOR)){
								//roomName = "teacher";
								//showRoom = false;
								if(int(objJoiner.@userid) < AppUserDataInfo.getInstance().userId)
									roomName = objInformation.username+"-"+objLiveStream.streamName;
								else
									roomName = objLiveStream.streamName+"-"+objInformation.username;
							}/*else if(objJoiner.@permission == AccessPermission.ADMINISTRATOR && objInformation.permission == AccessPermission.ADMINISTRATOR){
								roomName = "administrator";
							}*/else if((objJoiner.@permission == AccessPermission.ADMINISTRATOR && objInformation.permission == AccessPermission.INSTRUCTOR) ||
								(objJoiner.@permission == AccessPermission.ADMINISTRATOR && objInformation.permission == AccessPermission.STUDENT) ||
								(objJoiner.@permission == AccessPermission.INSTRUCTOR && objInformation.permission == AccessPermission.STUDENT)){
								roomName = objInformation.username+"-"+objLiveStream.streamName;
							}else
								roomName = objLiveStream.streamName+"-"+objInformation.username;

							//Alert.show("room name 2: "+roomName);
							// create one on one chat
							if(showRoom && (objInformation.permission == AccessPermission.INSTRUCTOR || 
								objInformation.permission == AccessPermission.ADMINISTRATOR ||
								objJoiner.@permission == AccessPermission.ADMINISTRATOR || 
								objJoiner.@permission == AccessPermission.INSTRUCTOR)){		
								
								objLiveStream.setStyle("verticalGap",3);
								
								objLiveStream.addChatControls(roomName,objLiveStream.streamName);
								
							}
						}
						
					} 
				} 
				else
					objLiveStream.available = false;
				//Save parent container
				objLiveStream.objParent = objContainer;
				
				//for selection rectangle
				objLiveStream.rubberBand = Application.application.objRubberBand;
				//position of window
				objLiveStream.x = i * 5 * MyObject.PADDING;
				objLiveStream.y = i * 5 * MyObject.PADDING;
				objLiveStream.addEventListener(MouseEvent.CLICK, onMouseClick);
				lstLiveStream.addItem(objLiveStream);
				objContainer.addChild(objLiveStream);
				
				
				/**/
			}
			//Application.application.cvsRoom.updateLayOut();
		}
		
		//When window is click --> set on top 
		private function onMouseClick(e: Event) {
			objContainer.setChildIndex(e.currentTarget as PlayLive, objContainer.numChildren-1);
		}
		
		//Start get all streaming
		public function playAll() {
			crrPos = 0;
			(lstLiveStream[crrPos] as PlayLive).play();
		}
		
		//Play/Get next streaming
		public function playNext() {
			crrPos++;
			if (crrPos < objInformation.maxJoiner)
				(lstLiveStream[crrPos] as PlayLive).play();			
			else
				onComplete(true);
		}
		
		//Stop all streaming
		public function stopAll() {
			var i: int;
			for (i=1; i<objInformation.maxJoiner; i++) {
				(lstLiveStream[i] as PlayLive).stop();
				(lstLiveStream[i] as PlayLive).removeEventListener(MouseEvent.CLICK, onMouseClick);
				//remove video window
				objContainer.removeChild(lstLiveStream[i]);
			}
			objContainer.removeAllChildren();
			lstLiveStream.removeAll();
			objInformation.lstVideoDisplay.removeAll();
		}

		//receive status of connection	    
		public function netStatus ( event:NetStatusEvent ):void
	    {
	    	if (event)
		        CommonUtils.log(event.info.code);
		    else
		        CommonUtils.log("This is empty position");
		    if (crrPos >= objInformation.maxJoiner) return;
			if ((lstLiveStream[crrPos] as PlayLive).available) {
		        if ( event.info.code == "NetStream.Play.Start" ) {
		        	(lstLiveStream[crrPos] as PlayLive).objVideoDsp.visible = true;
		        	(lstLiveStream[crrPos] as PlayLive).isPlaying = true;
					//Next				
					playNext();
		    	}
		    	if ( event.info.code == "NetStream.Play.Reset" ) {
		    		
		    	}
			}
			else {
				//Next				
				playNext();				
			}			
	    }
	    
	    //Auto position video windows
	    public function autoarrange():void {
	    	var i: int;
			var yD: Number = (objContainer.height - MyObject.PADDING)/(MyObject.MAX_ROW);
			var iTotalX =  Math.ceil(objInformation.maxJoiner/MyObject.MAX_ROW);
			var xD: Number = (objContainer.width - MyObject.PADDING)/iTotalX ;
			
			var cnt: int = 0;
    		for (i=0; i<lstLiveStream.length; i++) {
				var objPlayLive: PlayLive = lstLiveStream.getItemAt(i) as PlayLive;
				if (objPlayLive.available) {
					objPlayLive.x = (cnt % iTotalX) * xD + MyObject.PADDING;
					objPlayLive.y = Math.floor(cnt / iTotalX) * xD + MyObject.PADDING;
					cnt++;
				}
    		}	    		
    		
	    }
		
		//Minimize all video windows
		public function minimizeAll() {
			var i: int;
    		for (i=0; i<lstLiveStream.length; i++) {
				var objPlayLive: PlayLive = lstLiveStream.getItemAt(i) as PlayLive;
				objPlayLive.forceMinimize();
			}
		}
		
		//Maximize all video windows
		public function maximizeAll() {
			var i: int;
    		for (i=0; i<lstLiveStream.length; i++) {
				var objPlayLive: PlayLive = lstLiveStream.getItemAt(i) as PlayLive;
				objPlayLive.forceMaximize();
			}
		}		
			    
	}
}